// 组合form对象
export function combinationFormObject(form) {
  var formObject = {};
  if (form != null) {
    var name = "";
    var value = "";
    for (var i = 0, len = form.length; i < len; i++) {
      name = form[i].name;
      value = form[i].value;
      if (name != null && name != "") {
        formObject[name] = value;
      }
    }
  }
  return formObject;
}

/*日期格式转换*/
export function formatData(dateLong,format) {
  if (dateLong != null) {
    var date = new Date(dateLong);
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (format == null || format == "yyyy-MM-dd") {
      return date.getFullYear() + "-" + (month < 10 ? ("0" + month) : month)  + "-" + (day < 10 ? ("0" + day) : day);
    } else if (format == "yyyy-MM-dd hh:mm") {
      var hours = date.getHours();
      var minutes = date.getMinutes();
      return date.getFullYear() + "-" + (month < 10 ? ("0" + month) : month)  + "-" + (day < 10 ? ("0" + day) : day)
        + " " + (hours < 10 ? ("0" + hours) : hours) + ":" + (minutes < 10 ? ("0" + minutes) : minutes);
    } else {
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();
      return date.getFullYear() + "-" + (month < 10 ? ("0" + month) : month)  + "-" + (day < 10 ? ("0" + day) : day)
        + " " + (hours < 10 ? ("0" + hours) : hours) + ":" + (minutes < 10 ? ("0" + minutes) : minutes) + ":" + (seconds < 10 ? ("0" + seconds) : seconds);
    }
  }
}

/*时间差(分钟数)*/
export function timeDifference(beginDateLong,endDateLong) {
  var num;
  if (beginDateLong != null && endDateLong != null) {
    var beginDate = new Date(beginDateLong);
    var endDate = new Date(endDateLong);
    num = (endDate.getTime() - beginDate.getTime()) / (1000 * 60);
  }
  return num;
}

/*转换文件名*/
export function convertFileName(str) {
  var names = [];
  if (str != null && str != null) {
    var pathNames = str.split(";");
    for (var i = 0,len = pathNames.length;i < len;i++) {
      names.push(getFileName(pathNames[i]));
    }
  }
  return names;
}

/*拆分文件名*/
export function splitFileName(str) {
  var names = [];
  if (str != null && str != null) {
    var pathNames = str.split(";");
    for (var i = 0,len = pathNames.length;i < len;i++) {
      names.push(pathNames[i]);
    }
  }
  return names;
}

/*获取文件名称*/
export function getFileName(filePath) {
  var re = /\//;
  var re2 = /\\/;
  var fileName = "";
  if (filePath != null && filePath != "") {
    if (filePath.match(re2)) { //路径包含正斜杠符号
      fileName = filePath.substr(filePath.lastIndexOf("\\") + 1);
    } else if (filePath.match(re)) {
      fileName = filePath.substr(filePath.lastIndexOf("/") + 1);
    } else {
      fileName = filePath;
    }
  }
  return fileName;
}

/*获取文件后缀*/
export function getFileSuffix(fileName) {
  if (fileName == null || fileName == "") {
    console.log("获取文件后缀异常：传入文件名为空（" + fileName + "）");
    return "";
  }
  var index = fileName.lastIndexOf(".");
  if (index < 0) {
    console.log("获取文件后缀异常：传入文件名格式错误（" + fileName + "）");
    return "";
  }
  return fileName.substr(index);
}

//  格式化文件大小
export function renderSize(value){
  if(null==value||value==''){
    return "0 Bytes";
  }
  var unitArr = new Array("Bytes","KB","MB","GB","TB","PB","EB","ZB","YB");
  var index=0,srcsize = parseFloat(value);
  index=Math.floor(Math.log(srcsize)/Math.log(1024));
  var size =srcsize/Math.pow(1024,index);
  //  保留的小数位数
  size=size.toFixed(2);
  return size+unitArr[index];
}

/*过滤包含src标签的数据*/
export function filterSrcContentLabel(html) {
  if (html != null)
  {
    var srcReg = /<(img|video|video|meta).*?(?:>|\/>)/gi;
    var arr = html.match(srcReg);
     if (arr != null)
    {
      for (let i = 0,len = arr.length;i < len;i++) {
        html = html.replace(arr[i],"");
      }
    } else {
      html = html;
    }
    html=html.replace(/<p><br\/><\/p>/g,"");
    html=html.replace(/<p><\/p>/g,"");
  	html=html.replace(/<p .*?><\/p>/,"");
  }
  return html;
}

/*文件全局过滤*/
export function fileGlobalFilter(files) {
  if (files != null) {
    let fileSize = 100 * 1024 * 1000; //100M
    let fileType = Object.prototype.toString.call(files);
    if (fileType === "[object FileList]") { //对象是数组
      for (let i = 0,len = files.length;i < len;i++) {
        if (files[i].size > fileSize) {
          layer.msg("上传文件不能大于100M！", {
            time: 3000, //3s后自动关闭
          });
          return false;
        }
      }
    } else if(fileType === "[object File]"){ //单个文件
      if (files.size > fileSize) {
        layer.msg("上传文件不能大于100M！", {
          time: 3000, //3s后自动关闭
        });
        return false;
      }
    } else { //不是文件对象
      layer.msg("不是文件对象！", {
        time: 3000, //3s后自动关闭
      });
      return false;
    }
    return true;
  } else {
    layer.msg("请选择文件！", {
      time: 3000, //3s后自动关闭
    });
    return false;
  }
}
/*获取地址栏参数*/
export function getParamValue(name){
	if(window.location.href.split("?")[1]){
		var arrStr=window.location.href.split("?")[1].split("&");
		for(var i=0;i<arrStr.length;i++){
			if(arrStr[i].indexOf(name)>-1){
				return  arrStr[i].split("=")[1];
			}
		}
	}
	return;
}